/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function( $, common, ready, template, Collection, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function(_args) {
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            args.status = 'pool';
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'applys',
                condition: args,
                model: {
                    tag_name: 'tr',
                    url: {
                        put: _global.url.api + 'apply/{id}'
                    },
                    event: {
                        '.item-online:click': 'online',
                        '.item-offline:click': 'offline',
                        '.item-memo:click' : 'memo',
                        '.status-myself:click': 'statusChange'
                    },
                    online: function(e) {
                        e.preventDefault();
                        this.data.status = 1;
                        this.formData = {status: 1};
                        this.put();
                    },
                    statusChange: function (e) {
                        var model = this;
                        model.data.status = -3;
                        this.changeStatus(model, e);
                    },
                    changeStatus: function (model, e) {
                        if (e !== '') {
                            dialog({
                                content: '确定要继续吗？',
                                ok: function () {
                                    model.url = window._global.url.api + 'apply_status';
                                    model.put();
                                    _this.collection.fetch();
                                }, cancel: function () {
                                }
                            }).show(e.target);
                        } else {
                            model.url = window._global.url.api + 'apply_status';
                            model.put();
                            _this.collection.fetch();
                        }
                    },
                    offline: function(e) {
                        e.preventDefault();
                        this.data.status = 2;
                        this.formData = {status: 2};
                        this.put();
                    },
                    memo: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            content: template('memo-tpl', {memo: this.data.memo}),
                            ok: function() {
                                model.formData = {memo: $('textarea[name="memo"]').val()};
                                model.put();
                            },cancel: function() {}
                        }).show(e.target);
                    },
                    change: function(e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});